#pragma once
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <vector>
#include <string>

#include "Log.hpp"
#include "Err.hpp"

class Utility
{
public:
    static bool SetNonBlock(int fd)
    {
        int fl = fcntl(fd,F_GETFL);
        if(fl < 0)
        {
            logMessage(Fatal,"fcntl fatal,code:%d,string:%s",errno,strerror(errno));
           return false; 
        }
        fcntl(fd,F_SETFL,fl | O_NONBLOCK);
        return true;
    }
    // 切割函数
    //"1 + 2"
    static bool StringSplit(const std::string &str, const std::string &sep, std::vector<std::string> *result)
    {
        int start = 0;
        size_t pos = str.find(sep);
        while (pos != std::string::npos)
        {
            result->push_back(str.substr(start, pos - start));
            start = pos + sep.size();
            pos = str.find(sep,pos+1);
        }
        if (start < str.size())
            result->push_back(str.substr(start, str.size() - start)); // 防止漏掉最后一个数据
        return true;
    }
    static int ToInt(const std::string str)
    {
        return atoi(str.c_str());
    }
};
